<?php
class ccdsIniParse
{
//-----------------------------------------------------------------------------------------------------------------------
    private $sFileName;
    private $config=array();

//-----------------------------------------------------------------------------------------------------------------------
    function __construct( $sFileName )
    {
        if ( !file_exists( $sFileName ) )
            return trigger_error( "ccdsIniParse: File does not exists" );
        $handle=fopen( $sFileName, "r" );

        while ( $line=fgets( $handle ) )
        {
            $line=preg_replace( "/(;.*)/", "", $line );
            $line=trim( $line );

            if ( "" != $line )
            {
                list( $key, $value )=split( "=", $line );
                $key=trim( $key );
                $value=trim( $value );
                $value=preg_replace( '/^(")/', '', $value );
                $value=preg_replace( '/(")\B/', '', $value );
                $this->config[$key]=$value;
            }
        }
        fclose ($handle);
    }

//-----------------------------------------------------------------------------------------------------------------------
    function getArray( )
    {
        return $this->config;
    }
//-----------------------------------------------------------------------------------------------------------------------

}

class ccdsXMLparser
{

//-----------------------------------------------------------------------------------------------------------------------
    private $xml;

    function __construct( $sFileName )
    {
        if ( !file_exists( $sFileName ) )
            return; // trigger_error("ccdsIniParse: File does not exists");

        $this->xml=@simplexml_load_file( $sFileName, null, LIBXML_NOCDATA );
    }

//-----------------------------------------------------------------------------------------------------------------------
    function getArray( )
    {
        return $this->object2array( $this->xml );
    }
//-----------------------------------------------------------------------------------------------------------------------

    function object2array( $object )
    {
        $return=NULL;

        if ( is_array( $object ) )
        {
            foreach ( $object as $key => $value )
                $return[$key]=$this->object2array( $value );
        }
        else
        {
            $var=get_object_vars( $object );

            if ( $var )
            {
                foreach ( $var as $key => $value )
                    $return[$key]=$this->object2array( $value );
            }
            else
                return (string)$object; // strval and everything is fine
        }
        return $return;
    }
//-----------------------------------------------------------------------------------------------------------------------
}

class ccdsXMLgenerator
{
//-----------------------------------------------------------------------------------------------------------------------
    function getRoot( $root, $recursion )
    {
        $response="";

        if ( $root != "" )
        {
            for ( $i=0; $i < $recursion - 1; $i++ )
            {
                $response.="\t";
            }
            $response.="<{$root}>\n";
        }
        return $response;
    }

//-----------------------------------------------------------------------------------------------------------------------
    function getRootClose( $root, $recursion )
    {
        $response="";

        if ( $root != "" )
        {
            for ( $i=0; $i < $recursion - 1; $i++ )
            {
                $response.="\t";
            }
            $response.="</{$root}>\n";
        }
        return $response;
    }

//-----------------------------------------------------------------------------------------------------------------------
    function generateXML( $array, $root, $recursion=0, $numeric=false )
    {
        if ( !$numeric )
            $response.=$this->getRoot( $root, $recursion );
        $array_length=count( $array );
        $counter=0;

        foreach ( $array as $key => $value )
        {
            if ( is_array( $value ) )
            {
                if ( is_numeric( $key ) )
                {
                    if ( $counter > 0 )
                        $response.=$this->getRoot( $root, $recursion );
                    $response.=$this->generateXML( $value, $key, $recursion, true );

                    if ( $counter < $array_length - 1 )
                        $response.=$this->getRootClose( $root, $recursion );
                }
                else
                {
                    $response.=$this->generateXML( $value, $key, $recursion + 1 );
                }
            }
            else
            {
                for ( $i=0; $i < $recursion; $i++ )
                {
                    $response.="\t";
                }

                if ( is_numeric( $key ) )
                {
                    $response.="<{$root}>{$value}</{$root}>\n";
                }
                else
                {
                    $response.="<{$key}>{$value}</{$key}>\n";
                }
            }
            $counter++;
        }

        if ( !$numeric )
            $response.=$this->getRootClose( $root, $recursion );
        return $response;
    }
//-----------------------------------------------------------------------------------------------------------------------

}
